package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Goal;
import com.entity.Task;
import com.entity.User;
import com.util.DBHelp;
import com.util.RowMapper;

public class TaskDao {

	private DBHelp<Task> db=new DBHelp<Task>();
	
	public void save(Task task){
		String sql="insert into t_task(id,tasktitle,taskcontent,starttime,stoptime,state,level,plan,userid,goalid,createtime,manager) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, task.getId(),task.getTaskTitle(),task.getTaskContent(),task.getStartTime(),task.getStopTime(),task.getState(),task.getLevel(),task.getPlan(),task.getUserid(),task.getGoalid(),task.getCreateTime(),task.getManager());
	}
	
	public List<Task> findTaskByGoalId(String goalId){
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_task.id,t_task.tasktitle,t_task.taskcontent,manager,state,plan,`level`,starttime,stoptime,t_task.createtime,t_task.userid,t_task.goalid,t_user.username,t_user.image,t_goal.goalname,t_goal.goalcontent ");
		sql.append("FROM t_task ");
		sql.append("LEFT JOIN t_user ON t_user.id = t_task.userid ");
		sql.append("LEFT JOIN t_goal ON t_goal.id = t_task.goalid ");
		sql.append("WHERE t_task.goalid = ? ");
		return db.executeQueryForList(sql.toString(), new TaskRowMapper(), goalId);
	}
	
	public List<Task> findTaskByUserId(String userId){
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_task.id,t_task.tasktitle,t_task.taskcontent,manager,state,plan,`level`,starttime,stoptime,t_task.createtime,t_task.userid,t_task.goalid,t_user.username,t_user.image,t_goal.goalname,t_goal.goalcontent ");
		sql.append("FROM t_task ");
		sql.append("LEFT JOIN t_user ON t_user.id = t_task.userid ");
		sql.append("LEFT JOIN t_goal ON t_goal.id = t_task.goalid ");
		sql.append("WHERE t_task.userid = ? ");
		return db.executeQueryForList(sql.toString(), new TaskRowMapper(), userId);
	}
	
	
	/*public List<Task> findByGoalId(String goalid){
		String sql="select id,tasktitle,taskcontent,starttime,stoptime,state,createtime,`level`,manager,plan,userid from t_task where goalid=?";
		return db.executeQueryForList(sql,new TaskRowMapper(), goalid);
	}
	*/
	private class TaskRowMapper implements RowMapper<Task>{

		public Task mapRow(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			Task t=new Task();
			t.setId(rs.getString("id"));
			t.setLevel(rs.getString("level"));
			t.setPlan(rs.getFloat("plan"));
			t.setCreateTime(rs.getString("createtime"));
			t.setStartTime(rs.getString("starttime"));
			t.setStopTime(rs.getString("stoptime"));
			t.setTaskContent(rs.getString("taskcontent"));
			t.setState(rs.getString("state"));
			t.setTaskTitle(rs.getString("tasktitle"));
			t.setManager(rs.getString("manager"));
			
			User u=new User();
			u.setId(rs.getString("userid"));
			u.setUserName(rs.getString("username"));
			u.setImage(rs.getString("image"));
			
			t.setUser(u);
			t.setUserid(rs.getString("userid"));
			
			Goal g=new Goal();
			g.setId(rs.getString("goalid"));
			g.setGoalName(rs.getString("goalname"));
			g.setGoalContent(rs.getString("goalcontent"));
			
			t.setGoal(g);
			t.setGoalid(rs.getString("goalcontent"));
			
			return t;
		}
	}

	public Task findTaskByTaskId(String taskId) {
		String sql="select id,tasktitle,taskcontent,starttime,stoptime,manager,state,createtime,`level`,plan,userid from t_task where id=?";
		return db.executeQueryForObject(sql, new TaskRowMapper() , taskId);
	}
	
	public boolean update(Task task) {
		// TODO Auto-generated method stub
		String sql = "update t_task set tasktitle=? taskcontent=? state=? starttime=? stoptime=? `level`=? plan=? userid=? manager=? where id = ?";
		return db.executeSQL(sql,task.getTaskTitle(),task.getTaskContent(),task.getState(),task.getStopTime(),task.getLevel(),task.getPlan(),task.getUserid(),task.getId(),task.getManager());
	}
}
